/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.server.action.domain;

import han.bifang.business.domain.DomainManager;
import han.bifang.common.entity.SoaDomain;
import han.bifang.common.entity.SoaDomainDetail;
import han.bifang.common.entity.SoaServiceBus;
import han.bifang.common.entity.SoaServiceCatalog;
import han.bifang.common.exception.BifangErrorCode;
import han.bifang.common.exception.BifangException;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.Domain;
import han.bifang.gwt.client.common.entity.ServiceBus;
import han.bifang.gwt.client.common.entity.ServiceCatalog;
import han.bifang.gwt.client.domain.DomainDetail;
import han.bifang.gwt.server.action.BifangGWTAction;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

/**
 * 
 * @Author Kent
 */
public class GetDomainDetailAction implements BifangGWTAction {
	
	@Override
	public ResponseObject execute(RequestObject req,HttpServletRequest request,HttpServletResponse response) throws BifangException {
		ResponseObject res = new ResponseObject();
		try {
			String id = (String)req.getRequest();
			//logger.info("id = " + id);
			SoaDomainDetail sdd = DomainManager.getDomainDetail(id);
			//logger.info(sdd.getBuses() + "\n" + sdd.getCatalog());
			DomainDetail dd = new DomainDetail();
			Domain d = new Domain();
			SoaDomain sd = sdd.getDomain();
			BeanUtils.copyProperties(d, sd);
			ArrayList bus = new ArrayList();
			for(int i = 0 , j = sdd.getBuses().size(); i < j ; i ++){
				ServiceBus sb = new ServiceBus();
				SoaServiceBus ssb =  (SoaServiceBus)sdd.getBuses().get(i);
				BeanUtils.copyProperties(sb, ssb);
				bus.add(sb);
			}
			ArrayList cat = new ArrayList();
			for(int i = 0 , j = sdd.getCatalog().size(); i < j ; i ++){
				ServiceCatalog sb = new ServiceCatalog();
				SoaServiceCatalog ssc = (SoaServiceCatalog)sdd.getCatalog().get(i);
				BeanUtils.copyProperties(sb, ssc);
				cat.add(sb);
			}
			dd.setDomain(d);
			dd.setBuses(bus);
			dd.setCatalog(cat);
			res.setCode("0");
			res.setResponse(dd);
		} catch (BifangException e) {
			throw e;	
		}catch (Exception e) {
			throw new BifangException(BifangErrorCode.UNKNOWN_ERROR,e.getMessage(),e);		
		}
		return res;
		
	}

}
